package com.VolunteerPlatform.Controller.loadfile;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by DoveXL on 2018/12/10.
 */

@Controller
@RequestMapping(value = "/Downloadfile/**")
public class DownloadController {

    @RequestMapping("/teaminfotemplate")
    public ResponseEntity<byte[]> testResponseEntity(HttpSession session) throws IOException {
        byte[] body=null;
        ServletContext sc=session.getServletContext();
        InputStream in=sc.getResourceAsStream("/files/TeaminfoTemplate.xlsx");
        body=new byte[in.available()];
        in.read(body);

        HttpHeaders headers=new HttpHeaders();
        headers.add("Content-Disposition","attachment;filename=TeaminfoTemplate.xlsx");

        HttpStatus statusCode=HttpStatus.OK;

        ResponseEntity<byte[]> response=new ResponseEntity<byte[]>(body,headers,statusCode);
        return response;
    }



    @RequestMapping("/teaminfoErrorExcel")
    public ResponseEntity<byte[]> DownloadErrorExcel(HttpSession session) throws IOException {
        byte[] body=null;
        ServletContext sc=session.getServletContext();
        String FileName = session.getAttribute("ErrorName").toString();
        InputStream in=sc.getResourceAsStream("/files/errormsg/"+FileName);
        body=new byte[in.available()];
        in.read(body);

        HttpHeaders headers=new HttpHeaders();
        headers.add("Content-Disposition","attachment;filename="+FileName);

        HttpStatus statusCode=HttpStatus.OK;

        ResponseEntity<byte[]> response=new ResponseEntity<byte[]>(body,headers,statusCode);
        return response;
    }

    @RequestMapping("/teaminfoExportExcel")
    public ResponseEntity<byte[]> DownloadExportExcel(HttpSession session) throws IOException {
        byte[] body=null;
        ServletContext sc=session.getServletContext();
        String FileName = session.getAttribute("ExportTeamInfoName").toString();
        InputStream in=sc.getResourceAsStream("/files/download/"+FileName);
        body=new byte[in.available()];
        in.read(body);

        HttpHeaders headers=new HttpHeaders();
        headers.add("Content-Disposition","attachment;filename="+FileName);

        HttpStatus statusCode=HttpStatus.OK;

        ResponseEntity<byte[]> response=new ResponseEntity<byte[]>(body,headers,statusCode);
        return response;
    }

    @RequestMapping("/bankinfoExportExcel")
    public ResponseEntity<byte[]> DownloadExportbankExcel(HttpSession session) throws IOException {
        byte[] body=null;
        ServletContext sc=session.getServletContext();
        String FileName = session.getAttribute("ExportBankInfoName").toString();
        InputStream in=sc.getResourceAsStream("/files/download/"+FileName);
        body=new byte[in.available()];
        in.read(body);

        HttpHeaders headers=new HttpHeaders();
        headers.add("Content-Disposition","attachment;filename="+FileName);

        HttpStatus statusCode=HttpStatus.OK;

        ResponseEntity<byte[]> response=new ResponseEntity<byte[]>(body,headers,statusCode);
        return response;
    }

    @RequestMapping("/PersoninfoExportExcel")
    public ResponseEntity<byte[]> DownloadExportPersonExcel(HttpSession session) throws IOException {
        byte[] body=null;
        ServletContext sc=session.getServletContext();
        String FileName = session.getAttribute("ExportPersonInfoName").toString();
        InputStream in=sc.getResourceAsStream("/files/download/"+FileName);
        body=new byte[in.available()];
        in.read(body);

        HttpHeaders headers=new HttpHeaders();
        headers.add("Content-Disposition","attachment;filename="+FileName);

        HttpStatus statusCode=HttpStatus.OK;

        ResponseEntity<byte[]> response=new ResponseEntity<byte[]>(body,headers,statusCode);
        return response;
    }





}
